home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / wtj008.zip / OBJECTS.ZIP / TESTNODE.CPP next >
C/C++ Source or Header  |  1992-05-17  |  2KB  |  117 lines

  1. /* This program implements a new objectless object
  2.    derived from TString. While the example is silly,
  3.    it illustrates the technique and issues involved
  4.    well.
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <iostream.h>
  9. #include <malloc.h>
  10. #include <string.h>
  11. #include <windows.h>
  12. #include "tstrings.h"
  13.  
  14.  
  15. typedef struct TStringNode   FAR &RStringNode;
  16. typedef struct TStringNode   FAR *PStringNode;
  17. typedef struct TStringNode {
  18.   TString snString;
  19.   PStringNode snNext;
  20. } TStringNode;
  21.  
  22. PStringNode head = NULL;
  23.  
  24. //destructor for TStringNode
  25. void FAR EXPORT PASCAL TStringNodeDone(RString sSelf)
  26. {
  27.   //do nothing
  28. }
  29.  
  30. //constructor for TStringNode
  31. int TStringNodeInit(LPSTR AString, RStringNode sSelf)
  32. {
  33.   sSelf.snString.sString = AString;
  34.   sSelf.snString.sGetString = TStringsGetString;
  35.   sSelf.snString.sDone = TStringNodeDone;
  36.   sSelf.snNext = NULL;
  37.   return 1;
  38. }
  39.  
  40. //return a pointer to the last node in linked list
  41. PStringNode last()
  42. {
  43.   PStringNode node = head;
  44.   while (node->snNext)
  45.     node = node->snNext;
  46.   return node;
  47. }
  48.  
  49. //add a new TStringNode to the linked list
  50. void add(char *s)
  51. {
  52.   if (head) {
  53.     PStringNode node, lastnode = last();
  54.     node = (PStringNode) farmalloc(sizeof(TStringNode));
  55.     TStringNodeInit(s, *node);
  56.     lastnode->snNext = node;
  57.   }
  58.   else {
  59.     head = (PStringNode) farmalloc(sizeof(TStringNode));
  60.     TStringNodeInit(s, *head);
  61.   }
  62. }
  63.  
  64. //display an LPSTR
  65. void far_cout(LPSTR s)
  66. {
  67.   while (*s)
  68.     cout << *s++;
  69.   cout << '\n';
  70. }
  71.  
  72. //display each element in linked list
  73. void dump()
  74. {
  75.   PStringNode node = head;
  76.   while (node) {
  77.     far_cout(node->snString.sGetString(*(PString) node));
  78.     node = node->snNext;
  79.   }
  80. }
  81.  
  82. //call done destructor for each element in list
  83. void done()
  84. {
  85.   PStringNode node = head;
  86.   while (node) {
  87.     node->snString.sDone(*(PString) node);
  88.     node = node->snNext;
  89.   }
  90. }
  91.  
  92.  
  93. #pragma argsused
  94. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  95.            LPSTR lpszCmdLine, int cmdShow)
  96. {
  97.  
  98.   _InitEasyWin();
  99.  
  100.   char s[255];
  101.   do {
  102.     cout << "Enter a string (or return to quit adding): ";
  103.     gets(s);
  104.     if (*s) {
  105.       //allocate memory for new string
  106.       char *s1 = (char *) malloc(strlen(s) + 1);
  107.       //copy
  108.       strcpy(s1, s);
  109.       //add to linked list
  110.       add(s1);
  111.     }
  112.   } while (*s);
  113.   dump(); //display
  114.   done(); //finish up
  115.   return 0;
  116. }
  117.